﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.TreatmentHistory
{
    public class HistoryAdd : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected TreatmentInfo TrmData;
        protected TreatmentHistoryInfo TrmHistoryData;

        protected new void HandleCommon()
        {
            this.AddLocation("治疗记录", "/TreatmentHistory/Index.aspx");
            this.AddLocation("添加治疗", "/TreatmentHistory/HistoryAdd.aspx");
            this.SetTitle("添加治疗");

            int trmid = RequestTools.GetQueryInt("trmid", 0);
            if(trmid <= 0)
            {
                this.AddErrLine("请选择治疗");
                return;
            }
            TrmData = TreatmentBLL.Instance.GetTreatmentInfo(trmid);
            TrmHistoryData = PatientTreatmentBLL.Instance.GetCurrentTreatment(PrData.Id, trmid);
            if (TrmData.Id <= 0)
            {
                this.AddErrLine("请选择治疗");
                return;
            }
            if (TrmHistoryData.Id <= 0)
            {
                this.Redirect("/TreatmentHistory/HistoryStart.aspx?trmid=" + trmid);
            }
        }

        protected new void HandlePost()
        {
            TreatmentHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = PatientTreatmentBLL.Instance.AddTreatmentRecorde(info);
            if (succ)
            {
                this.AddMsgLine("添加成功");
            }
            else
            {
                this.AddErrLine("添加失败");
            }
            this.SetUrl("/TreatmentHistory/List.aspx?trmid=" + TrmData.Id);
            this.SetMetaRefresh();
        }

        protected new void HandleGet()
        {
            this.AddDatePicker();
        }

        protected TreatmentHistoryInfo ValidateForm()
        {
            TreatmentHistoryInfo info = new TreatmentHistoryInfo();

            // 对时间进行判断
            string value = RequestTools.GetFormString("startdate");
            if (value == "")
            {
                this.AddErrLine("请填写开始时间");
                return null;
            }
            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }
            info.StartDate = date;

            info.UsePurpose = RequestTools.GetInt("usepurpose", -1);

            value = RequestTools.GetFormString("enddate");
            if (value == "")
            {
                this.AddErrLine("请填写停止时间");
                return null;
            }
            succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }
            info.EndDate = date;

            //对时间进行检查

            info.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            if (info.UsePurpose > 3 || info.UsePurpose < 0)
            {
                this.AddErrLine("请填写使用原因");
                return null;
            }

            info.StopReason = RequestTools.GetFormInt("stopreason", -1);
            if (info.StopReason > 3 || info.StopReason < 0)
            {
                this.AddErrLine("请填写停止原因");
                return null;
            }

            info.DataValue = RequestTools.GetFormDouble("dosage", -1.0);
            if (info.DataValue < 0)
            {
                this.AddErrLine("请填写正确的剂量 ");
                return null;
            }

            info.State = Constants.STOPED_USE;
            info.UpdateDate = DateTime.Now;
            info.TreatmentId = TrmData.Id;
            info.PRId = PrData.Id;

            return info;
        }
    }
}